package jsyf.oa.dao.psi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jsyf.oa.entity.psi.InventoryEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by Maqiuqiu on 2018/10/17.
 */
@Repository
public class InventoryDao extends BaseDao {

    public InventoryDao() {
        super.iBaseEntity = new InventoryEntity();
    }

    public List<InventoryEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<InventoryEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            InventoryEntity entity = (InventoryEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, InventoryEntity.class);
    }

    public List<JSONObject> getInventoryList(String userId,String condition, Integer requestPage, Integer pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append("  SELECT\n" +
                "  t_psi_inventory.ID AS inventoryId,\n" +
                "  t_psi_purchase.IN_REPO_DATE AS inRepoDate,\n" +
                "  t_psi_purchase.IN_USER_ID AS inUserId,\n" +
                "  t_psi_purchase.IN_USER_NAME AS inUserName,\n" +
                "  t_psi_inventory.GOODS_ID AS goodsId,\n" +
                "  t_psi_goods.`NAME` AS goodsName,\n" +
                "  t_psi_inventory.UNIT AS unit,\n" +
                "  t_sys_company.`NAME` AS companyName,\n" +
                "  t_psi_inventory.COUNT AS count,\n" +
                "  t_psi_inventory.ADDRESS AS address,\n" +
                "\tt_psi_inventory.UNIT AS goodsUnit,\n" +
                "\tt_psi_inventory.PRICE AS price,\n" +
                "\tt_psi_inventory.REQUIREMENTS AS requirements,\n" +
                "\tt_psi_inventory.EXTRA AS extra,\n" +
                "  t_psi_purchase.STATE AS state,\n" +
                "  t_psi_purchase.ID AS purchaseId,\n" +
                "  t_psi_purchase.AUDIT_FLAG AS auditFlag,\n" +
                "  t_psi_purchase.CURRENT_AUDITOR_ID AS currentAuditorId,\n" +
                "  t_psi_purchase.CURRENT_AUDITOR_NAME AS currentAuditorName,\n" +
                "  t_psi_repo_in_out.ID AS repoInOutId,\n" +
                "  t_psi_repo_in_out.DATE AS date,\n" +
                "  t_psi_repo_in_out.OUT_USER_NAME AS outUserName,\n" +
                "  t_psi_repo_in_out.STATE AS outState,\n" +
                "  t_psi_repo_in_out.AUDIT_FLAG AS repoAuditFlag,\n" +
                "  t_psi_repo_in_out.CURRENT_AUDITOR_ID AS repoCurrentAuditorId,\n" +
                "  t_psi_repo_in_out.CURRENT_AUDITOR_NAME AS repoCurrentAuditorName,\n" +
                "  t_psi_purchase.CREATE_USER_ID AS createUserId,\n" +
                "  t_psi_repo_in_out.OUT_USER_ID AS outUserId\n" +
                "FROM t_psi_inventory\n" +
                "LEFT JOIN t_psi_goods ON t_psi_inventory.GOODS_ID = t_psi_goods.ID\n" +
                "LEFT JOIN t_psi_purchase ON  t_psi_inventory.PURCHASE_ID = t_psi_purchase.ID\n" +
                "LEFT JOIN t_psi_repo_in_out ON t_psi_inventory.REPO_IN_OUT_ID = t_psi_repo_in_out.ID\n" +
                "LEFT JOIN t_sys_user ON t_psi_inventory.CREATE_USER_ID = t_sys_user.ID\n" +
                "LEFT JOIN t_sys_role ON t_sys_user.ROLE_ID = t_sys_role.ID\n" +
                "LEFT JOIN t_sys_depart ON t_sys_role.DEPART_ID = t_sys_depart.ID\n" +
                "LEFT JOIN t_sys_company ON t_sys_depart.COMPANY_ID = t_sys_company.ID\n" +
                "WHERE 1 = 1\n" +
                "AND (\n" +
                "    t_psi_purchase.IN_USER_ID = '"+userId+"'\n" +
                "    OR t_psi_repo_in_out.OUT_USER_ID = '"+userId+"'\n" +
                "    OR t_psi_purchase.CREATE_USER_ID = '"+userId+"'\n" +
                "    OR (t_psi_repo_in_out.ID IN (SELECT MAIN_ID FROM t_sys_audit WHERE AUDITOR_ID = '"+userId+"')\n" +
                "        AND t_psi_repo_in_out.AUDIT_FLAG IN ('1', '0'))\n" +
                "    OR (t_psi_repo_in_out.ID IN (SELECT MAIN_ID FROM t_sys_copier WHERE COPIER_ID = '"+userId+"')\n" +
                "\t      AND t_psi_repo_in_out.AUDIT_FLAG = '1')\n" +
                "\t  OR (t_psi_purchase.ID IN (SELECT MAIN_ID FROM t_sys_audit WHERE AUDITOR_ID = '"+userId+"')\n" +
                "        AND t_psi_purchase.AUDIT_FLAG IN ('1', '0'))\n" +
                "    OR (t_psi_purchase.ID IN (SELECT MAIN_ID FROM t_sys_copier WHERE COPIER_ID = '"+userId+"')\n" +
                "\t      AND t_psi_purchase.AUDIT_FLAG = '1')\n" +
                ") \n" +
                "    ");
        if (condition != null){
            sql.append(condition);
        }
        super.appendPageSql(sql, requestPage, pageSize);
        return super.queryForJsonList(sql.toString());
    }

}

